﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace stsdev.UserInterface.Common {
    public partial class SelectAppPool : Form {

        private string appPool;
        public string AppPool {
            get {
                return (string) appPoolListBox.SelectedItem;
            }
            set {
                appPool = value;
            }
        }

        public SelectAppPool() {
            InitializeComponent();
        }

        private void SelectAppPool_Load(object sender, EventArgs e) {
            LoadAppPools();

            if (!string.IsNullOrEmpty(appPool)) {
                appPoolListBox.SelectedItem = appPool;
            }
        }

        private void LoadAppPools() {
            DirectoryEntry appPools = new DirectoryEntry("IIS://Localhost/W3SVC/APPPOOLS");

            appPoolListBox.Items.Clear();
            foreach (DirectoryEntry appPool in appPools.Children) {
                appPoolListBox.Items.Add(appPool.Name);
            }
        }

        private void refreshButton_Click(object sender, EventArgs e) {
            LoadAppPools();
        }

        private void appPoolListBox_SelectedIndexChanged(object sender, EventArgs e) {
            okButton.Enabled = (appPoolListBox.SelectedIndex > -1);
        }

        private void okButton_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
